package com.example.zxc;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class shouye extends AppCompatActivity {
    ListView listView;
    ArrayAdapter<String> adapter;
    String aa[]={"城市：重庆","城市：苏州","城市：南京","城市：北京","城市：西安","城市：上海"};
    String hh[]={"特色：火锅","特色：苏绣","特色：盐水鸭","特色：烤鸭","特色：肉夹馍","特色：丝绸"};
    int icon[]={R.drawable.tu1,R.drawable.tu2,R.drawable.tu3,R.drawable.tu4,R.drawable.tu5,R.drawable.tu6};
    final String[] message = new String[] {
            "重庆火锅介绍：   重庆火锅,又称为毛肚火锅或麻辣火锅,起源于明末清初的重庆嘉陵江畔、朝天门等码头船工纤夫的粗放餐饮方式,原料主要是牛毛肚、猪黄喉、鸭肠、牛血旺等。",
            "苏州苏绣介绍：   苏绣即苏州刺绣，其发源地在苏州吴县一带，现已遍衍江苏省的无锡、常州、扬州、宿迁、东台等地，苏绣产地江苏土地肥沃，气候温和，蚕桑发达，盛产丝绸，自古以来就是锦绣之乡。",
            "南京盐水鸭介绍：   南京盐水鸭制作历史悠久，积累了丰富的制作经验。鸭皮白肉嫩、肥而不腻、香鲜味美，具有香、酥、嫩的特点。而以中秋前后，桂花盛开季节制作的的盐水鸭色味最佳，名为桂花鸭。",
            "北京烤鸭介绍：   北京烤鸭享誉海内外,历史悠久,距今已经160多年,号称天下第一吃,是清代宫廷御菜且老少皆宜的保健美食!现以北京前门大街的全聚德饭店挂炉烤鸭与便宜坊饭店所制焖炉烤鸭为佳。",
            "西安肉夹馍介绍：   西安肉夹馍与白吉馍不同，其馍外观焦黄，条纹清晰，内部呈层状，饼体发胀，皮酥里嫩，火功到家，食用时温度以烫手为佳，且西安肉夹馍是热馍夹凉肉，饼酥肉香，爽而不腻。",
            "上海丝绸介绍：   上海是中国丝绸的重要产地和出口地之一。绸、缎、绫、罗、锦、绉、绒、葛、绨、纺、纱、绡应有尽有，花色繁多，深受国内外消费者的赞赏。" };
    private Gallery pictureGallery = null;
    private int index = 0;
    private float density = 0;
    private int[] picture = {
            R.drawable.tu1,
            R.drawable.tu2,
            R.drawable.tu3,
            R.drawable.tu4,
            R.drawable.tu5,
            R.drawable.tu6,
    };
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 去除手机界面默认标题
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_one);
        // 手机界面标题设置
        listView=(ListView)findViewById(R.id.listView);
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("icon", R.drawable.tu1);
        map1.put("name", aa[0]);
        map1.put("special",hh[0]);
        data.add(map1);

        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("icon", R.drawable.tu2);
        map2.put("name", aa[1]);
        map2.put("special",hh[1]);
        data.add(map2);

        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("icon", R.drawable.tu3);
        map3.put("name", aa[2]);
        map3.put("special",hh[2]);
        data.add(map3);

        Map<String, Object> map4 = new HashMap<String, Object>();
        map4.put("icon", R.drawable.tu4);
        map4.put("name", aa[3]);
        map4.put("special",hh[3]);
        data.add(map4);

        Map<String, Object> map5 = new HashMap<String, Object>();
        map5.put("icon", R.drawable.tu5);
        map5.put("name", aa[4]);
        map5.put("special",hh[4]);
        data.add(map5);

        Map<String, Object> map6 = new HashMap<String, Object>();
        map6.put("icon", R.drawable.tu6);
        map6.put("name", aa[5]);
        map6.put("special",hh[5]);
        data.add(map6);

        this.pictureGallery = (Gallery) findViewById(R.id.gallery);
        ImageAdapter adapter = new ImageAdapter(this);
        this.pictureGallery.setAdapter(adapter);
        Timer timer = new Timer();
        timer.schedule(task, 2000, 2000);
        this.parentControl();
        listView.setAdapter(new SimpleAdapter(this, data, R.layout.listview_layout,new String[] { "icon", "name","special" }, new int[] { R.id.imageView,R.id.textView,R.id.textView2}));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Bundle bundle = new Bundle();
                bundle.putInt("icon", icon[arg2]);
                bundle.putString("message", message[arg2]);
                Intent intent = new Intent();
                intent.putExtras(bundle);
                intent.setClass(shouye.this, MoveList.class);
                Log.i("message", message[arg2]);
                startActivity(intent);
                Toast.makeText(shouye.this, "正在跳转页面", Toast.LENGTH_SHORT).show();
            }
        });//监听器
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view= LayoutInflater.from(shouye.this).inflate(R.layout.listview_layout,null);
        TextView tv_name=(TextView)view.findViewById(R.id.textView);
        tv_name.setText(aa[position]);
        TextView tv_cai=(TextView)view.findViewById(R.id.textView2);
        tv_cai.setText(hh[position]);
        ImageView imageView=(ImageView)view.findViewById(R.id.imageView);
        imageView.setImageResource(icon[position]);
        return view;
    }
    private TimerTask task = new TimerTask() {

        @Override
        public void run() {
            Message message = new Message();
            message.what = 2;
            index = pictureGallery.getSelectedItemPosition();
            index++;
            handler.sendMessage(message);
        }
    };
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 2:
                    pictureGallery.setSelection(index);
                    break;
                default:
                    break;
            }
        }

    };
    private void parentControl() {//获取密度
        this.getDensity();
    }
    private void getDensity() {
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        density = metrics.density;
    }
    class ImageAdapter extends BaseAdapter {
        private int GalleryItemBackground;
        private Context context ;
        public ImageAdapter(Context context ){
            this.context = context ;
            TypedArray typedArray = context.obtainStyledAttributes(R.styleable.Gallery);
            GalleryItemBackground = typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
        }
        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }
        @Override
        public Object getItem(int position) {
            return position;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(picture[position % picture.length]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT));
            imageView.setBackgroundResource(GalleryItemBackground);
            return imageView;
        }

        private class Gallery_android_galleryItemBackground {
        }
    }
}

